<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\GoodsEvaluates;
use Illuminate\Http\Request;

class EvaluateController extends Controller {

    /**
     * 页面
     * @return view
     */
    public function index(){
        return view('admin.evaluate.index');
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request){
        try{
            $params = [
                'kwd' => (string)$request->get('kwd',''),
                'status' => (int)$request->get('status',0),
                'start' => (string)$request->get('start',''),
                'end' => (string)$request->get('end',''),
            ];
            $lists = GoodsEvaluates::getList($params);

            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 详情
     * @param Request $request
     * @return bool|view
     */
    public function detail(Request $request){
        try{
            $id = (int)$request->get('id',0);
            $info = [];
            if($id){
                $info = GoodsEvaluates::findOrFail($id);
                //评价信息
                if(!empty($info)){
                    //状态处理
                    $info['status_text'] = $info['status'] == 1 ? '正常' : '禁止显示';
                    if($info['average_star'] == 1){
                        $info['average_star_text'] = '好评';
                    }elseif ($info['average_star'] == 2){
                        $info['average_star_text'] = '中评';
                    }elseif ($info['average_star'] == 3){
                        $info['average_star_text'] = '差评';
                    }
                }else{
                    $info = array();
                }
            }
            $info = json_encode($info);

            return view('admin.evaluate.evaluate_detail',compact('info','id'));

        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     * 改变状态
     * @param Request $request
     * @return bool
     */
    public function status(Request $request){
        try{
            $id = (int)$request->get('id',0);
            $status = (int)$request->get('status',0);
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            $info = GoodsEvaluates::findOrFail($id);
            $info->update(['status' => $status]);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }
    /**
     * 单个/多个删除
     * @param Request $request
     * @return bool
     */
    public function destroy(Request $request){
        try{
            $id = $request->get('id');
            if(empty($id)){
                return $this->writeJson(1,null,'参数错误');
            }
            if(!is_array($id)){
                $id=[$id];
            }
            //修改软删除 19-7-31
            $data['status'] = -1;
            GoodsEvaluates::whereIn('id',$id)->update($data);
            return $this->writeJson(0);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

}
